package mobidever.mygodutch.bussiness;

import java.util.List;

import android.R.integer;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import mobidever.mygodutch.R;
import mobidever.mygodutch.bussiness.base.BussinessBase;
import mobidever.mygodutch.database.sqlitedal.SQLiteDALPayout;
import mobidever.mygodutch.model.ModelPayout;

public class BussinessPayout extends BussinessBase
{
	private SQLiteDALPayout mSqLiteDALPayout;

	public BussinessPayout(Context pContext)
	{
		super(pContext);
		mSqLiteDALPayout = new SQLiteDALPayout(pContext);
	}

	public boolean insertModelPayout(ModelPayout pModelPayout)
	{
		return mSqLiteDALPayout.insertPayout(pModelPayout);

	}

	public boolean deleteModelPayoutByID(int pID)
	{
		String _Condition = "PayoutID = " + pID;
		return mSqLiteDALPayout.deletePayout(_Condition);
	}

	public boolean deletePayoutByAccountBookID(int pAccountID)
	{
		String _Condition = "AccountBookID = " + pAccountID;
		return mSqLiteDALPayout.deletePayout(_Condition);
	}

	public boolean updateModelPayoutByID(ModelPayout pModelPayout)
	{
		return mSqLiteDALPayout.updatePayout(pModelPayout);
	}

	public List<ModelPayout> getPayouts(String pCondtition)
	{
		return mSqLiteDALPayout.getPayouts(pCondtition);
	}

	public ModelPayout getModelPayoutByPayoutID(int pPayoutID)
	{
		List<ModelPayout> _List = getPayouts("and PayoutID=" + pPayoutID);
		if (_List.size() == 1)
		{
			return _List.get(0);
		} else
		{
			return null;
		}
	}

	public boolean IsExistPayoutByPayoutName(String pPayoutName,
			Integer pPayoutID)
	{
		String _Condition = "and PayoutName='" + pPayoutName + "'";
		if (pPayoutID != null)
		{
			_Condition += "and PayoutID=" + pPayoutID;
		}
		List<ModelPayout> _List = getPayouts(_Condition);
		if (_List.size() == 1)
		{
			return true;
		} else
		{
			return false;
		}
	}

	public int getCount()
	{
		return mSqLiteDALPayout.getCount();
	}

	public List<ModelPayout> getPayoutByAccountBookID(int pAccountBookID)
	{
		String _Condition = "and AccountBookID = " + pAccountBookID
				+ " Order By PayoutDate DESC,PayoutID DESC";
		return getPayouts(_Condition);
	}

	public String getPayoutTotalMessage(String pPayoutDate, int pAccountBookID)
	{
		String _Total[] = getPayoutTotalByPayoutDate(pPayoutDate,
				pAccountBookID);
		return getContext().getString(R.string.TextViewTextPayoutTotal,
				new Object[]
				{ _Total[0], _Total[1] });
	}

	public String[] getPayoutTotalByPayoutDate(String pPayoutDate,
			int pAccountBookID)
	{
		String _Condition = " and PayoutDate = '" + pPayoutDate
				+ "' and AccountBookID = " + pAccountBookID;
		return getPayoutTotal(_Condition);
	}

	public String[] getPayoutTotalByAccountBookID(int pAccountBookID)
	{
		String _Condition = " and AccountBookID = " + pAccountBookID;
		return getPayoutTotal(_Condition);
	}

	public String[] getPayoutTotal(String pCondition)
	{
		String _sql = "select ifnull(Sum(Amount),0) as SumAmount,Count(Amount) as Count from Payout where 1=1 "
				+ pCondition;
		String _Total[] = new String[2];
		Cursor _Cursor = mSqLiteDALPayout.ExecSQL(_sql);
		if (_Cursor.getCount() == 1)
		{

			while (_Cursor.moveToNext())
			{
				_Total[0] = _Cursor.getString(_Cursor.getColumnIndex("Count"));
				_Total[1] = _Cursor.getString(_Cursor
						.getColumnIndex("SumAmount"));

			}

		}
		return _Total;
	}

	public List<ModelPayout> getPayoutOrderByPayoutUserID(String pCondition)
	{
		String _Condition = pCondition + "Order By PayoutUseID";
		List<ModelPayout> _List = getPayouts(_Condition);
		if (_List.size() > 0)
		{
			return _List;
		}
		return null;
	}

	public String[] getPayoutDateAndAmountTotal(String pCondition)
	{
		String _sql = "select Min(PayoutDate) as MinPayoutDate,Max(PayoutDate) as MaxPayoutDate, Sum(Amount) as Amount from Payout where 1=1"
				+ pCondition;
		String _Total[] = new String[3];
		Cursor _Cursor = mSqLiteDALPayout.ExecSQL(_sql);
		if (_Cursor.getCount() == 1)
		{
			
			
			
			while (_Cursor.moveToNext())
			{
				_Total[0] = _Cursor.getString(_Cursor
						.getColumnIndex("MinPayoutDate"));
				_Total[1] = _Cursor.getString(_Cursor
						.getColumnIndex("MaxPayoutDate"));
				_Total[2] = _Cursor.getString(_Cursor.getColumnIndex("Amount"));

			}
			
		}
		return _Total;
	}

}
